package cn.mumzone.pos.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsClassifyEntity;
import cn.mumzone.pos.model.SalesVolumeEntity;
import cn.mumzone.pos.model.SimpleEntity;
import cn.mumzone.pos.presenter.SalesVolumeReportPresenter;
import cn.mumzone.pos.presenter.contract.SalesVolumeReportContract;
import cn.mumzone.pos.ui.adapter.ADA_SalesVolume;
import cn.mumzone.pos.ui.adapter.ADA_SimpleList;

/**
 * author:        yupeng
 * created on:    2019/7/11 18:19
 * description:
 */
public class FRA_SalesVolume extends BaseFragment<SalesVolumeReportPresenter> implements SalesVolumeReportContract.View {
    private static final String ORDER_TYPE_GOODS = "0";
    private static final String ORDER_TYPE_SERVICE = "1";
    private static final String ORDER_TYPE_CARD = "2";
    private static final String ORDER_TYPE_REDEEM = "3";

    @BindView(R.id.tv_select_time_title)
    TextView tvSelectTimeTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.ll_time_select)
    LinearLayout llTimeSelect;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.ll_order_type)
    LinearLayout llOrderType;
    @BindView(R.id.tv_classify_one)
    TextView tvClassifyOne;
    @BindView(R.id.ll_classify_one)
    LinearLayout llClassifyOne;
    @BindView(R.id.tv_classify_two)
    TextView tvClassifyTwo;
    @BindView(R.id.ll_classify_two)
    LinearLayout llClassifyTwo;
    @BindView(R.id.tv_classify_three)
    TextView tvClassifyThree;
    @BindView(R.id.ll_classify_three)
    LinearLayout llClassifyThree;
    @BindView(R.id.tv_print)
    TextView tvPrint;
    @BindView(R.id.rv_sales_volume)
    RecyclerView rvSalesVolume;
    @BindView(R.id.tv_data_empty)
    TextView tvDataEmpty;

    public static FRA_SalesVolume newInstance() {
        return new FRA_SalesVolume();
    }

    private TimePickerView datePickDialog;

    private PopupWindow pwOrderType;

    private PopupWindow pwClassifyOne;

    private PopupWindow pwClassifyTwo;

    private PopupWindow pwClassifyThree;

    private ADA_SimpleList classifyOneAdapter;

    private ADA_SimpleList classifyTwoAdapter;

    private ADA_SimpleList classifyThreeAdapter;

    private ADA_SalesVolume adapter;

    private String selectDay;

    private String orderType = "";

    private String classifyOne = "";

    private String classifyTwo = "";

    private String classifyThree = "";

    @Override
    protected void initEventAndData() {

        rvSalesVolume.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ADA_SalesVolume(mContext);
        rvSalesVolume.setAdapter(adapter);

        initDatePickDialog();

        initOrderTypePopWindow();

        initClassifyOnePopWindow();

        initClassifyTwoPopWindow();

        initClassifyThreePopWindow();

        selectDay = AppDateUtil.getDateToString(System.currentTimeMillis(), AppDateUtil.YYYY_MM_DD);
        tvTime.setText(AppDateUtil.getDateToString(System.currentTimeMillis(), AppDateUtil.YYYY_MM_DD1));

        tvOrderType.setText("商品售卖");
        llClassifyOne.setVisibility(View.VISIBLE);
        llClassifyTwo.setVisibility(View.GONE);
        llClassifyThree.setVisibility(View.GONE);
        orderType = ORDER_TYPE_GOODS;

        mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
        mPresenter.requestGoodsClassify(classifyOne, classifyTwo, classifyThree);
    }

    private void initOrderTypePopWindow() {
        pwOrderType = new PopupWindow(mContext);
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(mContext).inflate(R.layout.view_select_pop, null);
        pwOrderType.setWidth(DisplayUtil.dip2px(mContext, 220));
        pwOrderType.setBackgroundDrawable(getResources().getDrawable(android.R.color.white));
        pwOrderType.setContentView(recyclerView);
        pwOrderType.setOutsideTouchable(true);

        List<SimpleEntity> orderTypeList = new ArrayList<>();
        orderTypeList.add(new SimpleEntity(ORDER_TYPE_GOODS, "商品售卖"));
        orderTypeList.add(new SimpleEntity(ORDER_TYPE_SERVICE, "服务售卖"));
        orderTypeList.add(new SimpleEntity(ORDER_TYPE_CARD, "次卡售卖"));
        orderTypeList.add(new SimpleEntity(ORDER_TYPE_REDEEM, "积分换购"));
        ADA_SimpleList simpleList = new ADA_SimpleList(mContext);
        simpleList.setDataList(orderTypeList);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(simpleList);
        simpleList.setOnItemClickListener(new ADA_SimpleList.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, SimpleEntity entity) {
                pwOrderType.dismiss();
                tvOrderType.setText(entity.getName());
                orderType = entity.getId();
                adapter.setIntegralGoods(orderType.equals(ORDER_TYPE_REDEEM));
                llClassifyOne.setVisibility(ORDER_TYPE_GOODS.equals(orderType) ? View.VISIBLE : View.GONE);
                llClassifyTwo.setVisibility(View.GONE);
                llClassifyThree.setVisibility(View.GONE);
                classifyOne = "";
                classifyTwo = "";
                classifyThree = "";
                tvClassifyOne.setText("");
                tvClassifyTwo.setText("");
                tvClassifyThree.setText("");
                mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);

            }
        });
    }

    private void initClassifyOnePopWindow() {
        pwClassifyOne = new PopupWindow(mContext);
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(mContext).inflate(R.layout.view_select_pop, null);
        pwClassifyOne.setWidth(DisplayUtil.dip2px(mContext, 220));
        pwClassifyOne.setHeight(DisplayUtil.dip2px(mContext, 300));
        pwClassifyOne.setBackgroundDrawable(getResources().getDrawable(android.R.color.white));
        pwClassifyOne.setContentView(recyclerView);
        pwClassifyOne.setOutsideTouchable(true);

        classifyOneAdapter = new ADA_SimpleList(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(classifyOneAdapter);
        classifyOneAdapter.setOnItemClickListener(new ADA_SimpleList.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, SimpleEntity entity) {
                pwClassifyOne.dismiss();
                llClassifyTwo.setVisibility(View.VISIBLE);
                llClassifyThree.setVisibility(View.GONE);
                tvClassifyOne.setText(entity.getName());
                classifyOne = entity.getId();
                classifyTwo = "";
                classifyThree = "";
                tvClassifyTwo.setText("");
                tvClassifyThree.setText("");
                mPresenter.requestGoodsClassify(classifyOne, classifyTwo, classifyThree);
                mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
            }
        });
    }

    private void initClassifyTwoPopWindow() {
        pwClassifyTwo = new PopupWindow(mContext);
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(mContext).inflate(R.layout.view_select_pop, null);
        pwClassifyTwo.setWidth(DisplayUtil.dip2px(mContext, 220));
        pwClassifyTwo.setHeight(DisplayUtil.dip2px(mContext, 300));
        pwClassifyTwo.setBackgroundDrawable(getResources().getDrawable(android.R.color.white));
        pwClassifyTwo.setContentView(recyclerView);
        pwClassifyTwo.setOutsideTouchable(true);

        classifyTwoAdapter = new ADA_SimpleList(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(classifyTwoAdapter);
        classifyTwoAdapter.setOnItemClickListener(new ADA_SimpleList.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, SimpleEntity entity) {
                pwClassifyTwo.dismiss();
                llClassifyThree.setVisibility(View.VISIBLE);
                tvClassifyTwo.setText(entity.getName());
                classifyTwo = entity.getId();
                classifyThree = "";
                tvClassifyThree.setText("");
                mPresenter.requestGoodsClassify(classifyOne, classifyTwo, classifyThree);
                mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
            }
        });
    }

    private void initClassifyThreePopWindow() {
        pwClassifyThree = new PopupWindow(mContext);
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(mContext).inflate(R.layout.view_select_pop, null);
        pwClassifyThree.setWidth(DisplayUtil.dip2px(mContext, 220));
        pwClassifyThree.setHeight(DisplayUtil.dip2px(mContext, 300));
        pwClassifyThree.setBackgroundDrawable(getResources().getDrawable(android.R.color.white));
        pwClassifyThree.setContentView(recyclerView);
        pwClassifyThree.setOutsideTouchable(true);

        classifyThreeAdapter = new ADA_SimpleList(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(classifyThreeAdapter);
        classifyThreeAdapter.setOnItemClickListener(new ADA_SimpleList.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, SimpleEntity entity) {
                pwClassifyThree.dismiss();
                tvClassifyThree.setText(entity.getName());
                classifyThree = entity.getId();
                mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
            }
        });
    }

    @Override
    protected void onUserVisible() {
        mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
        }
    }

    @Override
    public void showSalesVolume(List<SalesVolumeEntity> list) {

        adapter.setData(list);
        adapter.notifyDataSetChanged();

        tvDataEmpty.setVisibility(ListUtil.isListEmpty(list) ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showGoodsClassify(List<GoodsClassifyEntity> classifyEntities) {
        if (StringUtils.isEmpty(classifyOne)) {
            List<SimpleEntity> classifyOneList = new ArrayList<>();
            for (GoodsClassifyEntity entity : classifyEntities) {
                classifyOneList.add(new SimpleEntity(entity.getPrimaryClassify(), entity.getPrimaryName()));
            }
            classifyOneAdapter.update(classifyOneList, true);
        } else if (StringUtils.isEmpty(classifyTwo)) {
            List<SimpleEntity> classifyTwoList = new ArrayList<>();
            for (GoodsClassifyEntity entity : classifyEntities) {
                classifyTwoList.add(new SimpleEntity(entity.getSecondaryClassify(), entity.getSecondaryName()));
            }
            classifyTwoAdapter.update(classifyTwoList, true);
        } else if (StringUtils.isEmpty(classifyThree)) {
            List<SimpleEntity> classifyThreeList = new ArrayList<>();
            for (GoodsClassifyEntity entity : classifyEntities) {
                classifyThreeList.add(new SimpleEntity(entity.getThreeClassify(), entity.getThreeName()));
            }
            classifyThreeAdapter.update(classifyThreeList, true);
        }

    }

    private void initDatePickDialog() {

        Calendar nowDate = Calendar.getInstance();
        int nowYear = nowDate.get(Calendar.YEAR);

        Calendar startDate = Calendar.getInstance();
        startDate.set(nowYear - 2, 0, 1);
        Calendar endDate = Calendar.getInstance();

        datePickDialog = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                datePickDialog.dismiss();
                selectDay = AppDateUtil.getDateToString(date.getTime(), AppDateUtil.YYYY_MM_DD);
                mPresenter.requestSalesVolume(selectDay, classifyOne, classifyTwo, classifyThree, orderType);
                tvTime.setText(AppDateUtil.getDateToString(date.getTime(), AppDateUtil.YYYY_MM_DD1));
            }
        }).isDialog(true).isCyclic(false).setOutSideCancelable(true)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setDate(nowDate)
                .setRangDate(startDate, endDate)
                .build();
        datePickDialog.getDialog().getWindow().setLayout(DisplayUtil.dip2px(mContext, 500), WindowManager.LayoutParams.WRAP_CONTENT);

    }

    @OnClick({R.id.ll_time_select, R.id.ll_order_type, R.id.ll_classify_one, R.id.ll_classify_two, R.id.ll_classify_three})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_time_select:
                datePickDialog.show();
                break;
            case R.id.ll_order_type:
                pwOrderType.showAsDropDown(view);
                break;
            case R.id.ll_classify_one:
                pwClassifyOne.showAsDropDown(view);
                break;
            case R.id.ll_classify_two:
                pwClassifyTwo.showAsDropDown(view);
                break;
            case R.id.ll_classify_three:
                pwClassifyThree.showAsDropDown(view);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (datePickDialog != null) {
            datePickDialog.dismiss();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_sales_volume;
    }
}
